package org.myself.meeting.service;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.mapper.*;
import org.myself.meeting.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MeetingService {

    @Autowired
    MeetingMapper meetingMapper;

    @Autowired
    MeetingRoomMapper meetingRoomMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    MeetingParticipantsMapper meetingParticipantsMapper;

    @Autowired
    NewMessageMapper nmmp;

    public Integer addMeeting(Meeting meeting, Integer[] mps, Employee currentuser) {
        String operation = currentuser.getEmployeename()+" 添加会议 "+meeting.getMeetingname();
        nmmp.addNewMessage(Constant.UPGRADE,operation,new Date());
        meeting.setReservationtime(new Date());  //设置发起时间
        Integer result = meetingMapper.addMeeting(meeting);
        meetingMapper.addParticipants(meeting.getMeetingid(),mps);
        return result;
    }

    public long getTotal(Integer status) {
        return meetingMapper.getTotal(status);
    }
    public long getNameTotal(Integer status,String name) {
        return meetingMapper.getNameTotal(status,name);
    }

    public List<SearchMeeting> getAllmeetings(Integer page, Integer pagesize,Integer status) {
        page = (page-1)*pagesize;
        List<Meeting> mts = meetingMapper.getAllmeetings(page,pagesize,status);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        for (Meeting mt:mts ) {
            SearchMeeting smt = new SearchMeeting();
            smt.setMeetingid(mt.getMeetingid());
            smt.setMeetingname(mt.getMeetingname());
            smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
            smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
            smt.setNumberofparticipants(mt.getNumberofparticipants());
            smt.setStarttime(mt.getStarttime());
            smt.setEndtime(mt.getEndtime());
            smt.setReservationtime(mt.getReservationtime());
            smt.setCanceledtime(mt.getCanceledtime());
            smt.setDescription(mt.getDescription());
            smt.setStatus(mt.getStatus());
            smts.add(smt);
        }
        return smts;
    }


    public List<SearchMeeting> getMeetingsByName(Integer page, Integer pagesize,Integer status,String meetingname){
        page = (page-1)*pagesize;
        List<Meeting> mts = meetingMapper.getMeetingsByName(page,pagesize,status,meetingname);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        for (Meeting mt:mts ) {
            SearchMeeting smt = new SearchMeeting();
            smt.setMeetingid(mt.getMeetingid());
            smt.setMeetingname(mt.getMeetingname());
            smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
            smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
            smt.setNumberofparticipants(mt.getNumberofparticipants());
            smt.setStarttime(mt.getStarttime());
            smt.setEndtime(mt.getEndtime());
            smt.setReservationtime(mt.getReservationtime());
            smt.setCanceledtime(mt.getCanceledtime());
            smt.setDescription(mt.getDescription());
            smt.setStatus(mt.getStatus());
            smts.add(smt);
        }
        return smts;
    }

    public SearchMeeting getMetByIdSTATUS(Integer id,Integer status) {
        SearchMeeting smt = new SearchMeeting();
        Meeting mt = meetingMapper.getMetByIdSTATUS(id,status);
        if(mt != null){
            smt.setMeetingid(mt.getMeetingid());
            smt.setMeetingname(mt.getMeetingname());
            smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
            smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
            smt.setNumberofparticipants(mt.getNumberofparticipants());
            smt.setStarttime(mt.getStarttime());
            smt.setEndtime(mt.getEndtime());
            smt.setReservationtime(mt.getReservationtime());
            smt.setCanceledtime(mt.getCanceledtime());
            smt.setDescription(mt.getDescription());
            smt.setStatus(mt.getStatus());
        }
        return smt;
    }

    public List<Employee> getMetPerById(Integer id) {
        //查询返回对应的表   会议的id
        List<MeetingParticipants> mps =  meetingParticipantsMapper.getById(id);
        List<Employee> eps = new ArrayList<Employee>();
        for (MeetingParticipants mp:mps) {
            Employee ep = employeeMapper.getEmpByEmpId(mp.getEmployeeid());
            eps.add(ep);
        }
        return eps;
    }


    public Integer updateDesStatus(String description, Integer metstatus, Integer id, Employee emp) {
        String operation = emp.getEmployeename()+" 取消了一个会议 ";
        nmmp.addNewMessage(Constant.CREATEMEETING,operation,new Date());
        Date dd = new Date();
        return meetingMapper.updateDesStatus(description,metstatus,id,dd);
    }

    public Integer updateMetStatus(Integer meetingid, Integer status, Employee emp) {
        Meeting mt = meetingMapper.getMetById(meetingid);
        String operation = emp.getEmployeename()+" 更新了会议 "+mt.getMeetingname();
        nmmp.addNewMessage(Constant.CREATEMEETINGMANAGER,operation,new Date());
        return meetingMapper.updateMetStatus(meetingid,status);
    }

    public List<Meeting> getAllmeetingsAdmin() {

        return meetingMapper.getAllmeetingsAdmin();
    }

    public Meeting getMeetingById(Integer id) {
        return meetingMapper.getMetById(id);
    }

    public Integer updateDepAll(Meeting meeting) {
        meetingMapper.deleteMetById(meeting.getMeetingid());
        return meetingMapper.addMeeting(meeting);
    }

    public Integer deleteMetById(Integer id) {
        return meetingMapper.deleteMetById(id);
    }

    public Integer deletemeeting(Integer roomid) {
        return meetingMapper.deletemeeting(roomid);
    }
}
